package xin.marcher.module.replenished.test.mapper;

import junit.framework.TestCase;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xin.marcher.module.common.domain.BaseDO;
import xin.marcher.module.replenished.ReplenishedApplication;
import xin.marcher.module.replenished.domain.entity.SkuSellerRelationDO;
import xin.marcher.module.replenished.domain.entity.SkuStockSellerRelationDO;
import xin.marcher.module.replenished.mapper.SkuSellerRelationMapper;
import xin.marcher.module.replenished.mapper.SkuStockSellerRelationMapper;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 */
@SpringBootTest(classes = ReplenishedApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ForJmeterCreateMockData extends TestCase {

    public static final Range SKU_INFO_SKU_ID_RANGE = new Range(8_000_000_001L, 8_000_010_000L);

    public static final Range ITEM_INFO_ITEM_ID_RANGE = new Range(100_000_000_001L, 100_000_001_000L);

    public static final Range SELLER_GROUP_ID_RANGE = new Range(300_001, 301_000);

    public static final Range SELLER_INFO_ID_RANGE = new Range(20_000_001, 20_001_000);

    public static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();

    @Autowired
    private SkuSellerRelationMapper skuSellerRelationMapper;

    @Autowired
    private SkuStockSellerRelationMapper skuStockSellerRelationMapper;

    /**
     * sku_seller_relation 表
     */
    @Test
    public void skuSellerRelation() {
        List<Runnable> tasks = new ArrayList<>();
        for (Long skuId : SKU_INFO_SKU_ID_RANGE.asList()) {
            SkuSellerRelationDO entity = new SkuSellerRelationDO();
            entity.setSkuId(skuId.toString());
            Long itemId = ITEM_INFO_ITEM_ID_RANGE.randomOne();
            entity.setItemId(itemId.toString());
            entity.setSellerGroupId(SELLER_GROUP_ID_RANGE.randomOne());
            entity.setSellerType(computeSellerTypeById(itemId));
            entity.setRelationType(1/*可售*/);
            setCommonFieldValue(entity);
            tasks.add(() -> skuSellerRelationMapper.insert(entity));
        }
        executeTaskWithThreadPool(tasks);
    }

    /**
     * sku_stock_seller_relation 表
     */
    @Test
    public void skuStockSellerRelation() {
        List<Runnable> list = new ArrayList<>();
        List<Runnable> tasks = new ArrayList<>();
        for (Long skuId : SKU_INFO_SKU_ID_RANGE.asList()) {
            SkuStockSellerRelationDO entity = new SkuStockSellerRelationDO();
            entity.setSkuId(skuId.toString());
            entity.setSellerType(computeSellerTypeById(skuId));
            entity.setSellerId(SELLER_INFO_ID_RANGE.randomOne());
            entity.setStockNum(new BigDecimal("100"));
            entity.setStockUnit("盒");
            setCommonFieldValue(entity);
            tasks.add(() -> skuStockSellerRelationMapper.insert(entity));
        }
        executeTaskWithThreadPool(tasks);
    }

    private void setCommonFieldValue(BaseDO entity) {
        try {
            entity.setDelFlag(getYesInteger());
            entity.setCreateUser(getFixedInteger());
            entity.setCreateTime(DateUtils.parseDate("2022-04-02 00:00:00", "yyyy-MM-dd HH:mm:ss"));
            entity.setUpdateUser(getFixedInteger());
            entity.setUpdateTime(DateUtils.parseDate("2022-04-02 00:00:00", "yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException ignore) {
        }
    }

    private void executeTaskWithThreadPool(List<Runnable> tasks) {
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(100);
        for (Runnable task : tasks) {
            threadPoolExecutor.execute(task);
        }
        // 循环等待线程池里的任务都执行完
        while (threadPoolExecutor.getQueue().size() != 0) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 现在可以关闭线程池了
        threadPoolExecutor.shutdown();
    }

    private Integer computeSellerTypeById(Long id) {
        return (int) (id % 2) + 1;
    }

    private Integer getYesInteger() {
        return 1;
    }

    private String appendGrowingNum(String str) {
        return str + ATOMIC_INTEGER.incrementAndGet();
    }

    private Integer getFixedInteger() {
        return 1;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Range {
        // 前闭
        private long start;
        // 后也闭
        private long end;

        public List<Long> asList() {
            List<Long> list = new ArrayList<>();
            for (long i = start; i <= end; i++) {
                list.add(i);
            }
            return list;
        }

        public Long randomOne() {
            return RandomUtils.nextLong(start, end + 1);
        }
    }
}
